package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/albums")
@Api(tags = "1. 相册管理模块")
public class AlbumController {

    public AlbumController () {
        log.debug("创建控制器类对象: AlbumController");

    }
    @Autowired
    private IAlbumService albumService;

    // http://localhost:9080/albums/add-new?name=TestAlbum001&description=TestDescription001&sort=88
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的请求，参数：{}", albumAddNewDTO);
        albumService.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1,max = 10000,message = "删除相册失败，相册ID非法！")
                             @PathVariable Long id) {
        log.debug("开始【模拟】处理【根据id删除相册】的请求，参数：{}", id);
        albumService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id,AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理【修改相册详情】的请求，参数：{}",albumUpdateDTO);
        albumService.updateInfoById(id,albumUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9+]}")
    @ApiOperation("根据id查询数据详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "相册ID", required = true, example = "9", dataType = "long")
    public JsonResult<AlbumStandardVO> getStandardById(@Range(min = 1, max = 1000000, message = "查询相册详情失败，相册ID非法！")
                                          @PathVariable Long id){

        log.debug("开始处理【根据id查询相册详情】的请求，参数：{}",id);
        AlbumStandardVO albumStandardVO = albumService.getStandardById(id);
        return JsonResult.ok(albumStandardVO);
    }

    @GetMapping("")
    @ApiOperation("查询相册列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumListItemVO>> list(){
        log.debug("开始处理【查询相册列表】的业务，无参数");
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }
    // http://localhost:9080/albums/aaa
    @GetMapping("/{name:[a-z]+}")
    public String getStandardByName(@PathVariable String name) {
        return "根据Name【" + name + "】查询相册详情";
    }

    // http://localhost:9080/albums/test
    @GetMapping("/test")
    public String testStandardByName(String name) {
        return "【测试】根据Name【" + name + "】查询相册详情";
    }

}